﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Windows.Media.Animation;
using System.Windows.Controls.Primitives;

namespace _1vs100
{
    /// <summary>
    /// Interaction logic for QuestionWindow.xaml
    /// </summary>
    public partial class QuestionWindow : Window
    {
        Boolean isOpen;
        public Boolean selfClose;
        MediaPlayer correctPlayer, wrongPlayer, questionPlayer;
        Random r;
        public Boolean lifeLineSet;
        public Boolean statusBarSet;
        public ProgressBar pbTimer;

        public QuestionWindow()
        {
            InitializeComponent(); 
            isOpen = false;
            selfClose = false;
            correctPlayer = new MediaPlayer();
            wrongPlayer = new MediaPlayer();
            questionPlayer = new MediaPlayer();
            r = new Random();
            lifeLineSet = false;
            statusBarSet = false;
            pbTimer = new ProgressBar();
            setProperties();
            startQuestions();
        }

        /****************************************************Private Methods */
        private void setProperties()
        {
            generateMobIQ();
            isOpen = true;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.ResizeMode = ResizeMode.NoResize;
            this.ResizeMode = ResizeMode.CanMinimize;
            //tbTimer.Text = ((App)Application.Current).currentGame.currentRules.Timer.ToString();
            //tbTimer.Text += " Seconds";
            //tbTimer.TextAlignment = TextAlignment.Center;
            lblQuestion.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblAnswer1.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblAnswer2.HorizontalContentAlignment = HorizontalAlignment.Center;
            lblAnswer3.HorizontalContentAlignment = HorizontalAlignment.Center;
            btnAnswer1.Focusable = false;
            btnAnswer2.Focusable = false;
            btnAnswer3.Focusable = false;
        }

        private void startQuestions()
        {
            ((App)Application.Current).currentGame.currentQuestionFile.fillQuestion(((App)Application.Current).currentGame.currentRules.Difficulty);
        }

        public void showNextQuestion()
        {
            ((App)Application.Current).currentGame.currentQuestion = ((App)Application.Current).currentGame.currentQuestionFile.getNextQuestion();
            ((App)Application.Current).currentGame.currentAnswer = ((App)Application.Current).currentGame.currentQuestionFile.getCurrentAnswer();
            ((App)Application.Current).currentGame.currentChoices = ((App)Application.Current).currentGame.currentQuestionFile.getCurrentChoices();
            
            lblQuestion.Content = ((App)Application.Current).currentGame.currentQuestion;
            lblAnswer1.Content = ((App)Application.Current).currentGame.currentChoices[0];
            lblAnswer2.Content = ((App)Application.Current).currentGame.currentChoices[1];
            lblAnswer3.Content = ((App)Application.Current).currentGame.currentChoices[2];

            ((App)Application.Current).currentGame.currentQuestionFile.incrQ();
        }

        private void generateMobIQ()
        {
            for (int i = 0; i < ((App)Application.Current).currentGame.CurrentMob.Count; i++)
            {
                ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value._IQ = r.Next(1, 11);
            }

        }

        public void generateMobAnswers()
        {

            int i = 0;
            Boolean sucess = false;

            for (i = 0; i < ((App)Application.Current).currentGame.CurrentMob.Count; i++)
            {
                sucess = generateProbability(((App)Application.Current).currentGame.CurrentMob.ElementAt(i));
                if (sucess)
                {
                    ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer = ((App)Application.Current).currentGame.currentAnswer;
                }
                else
                {
                    if(((App)Application.Current).currentGame.currentAnswer.Equals("A"))
                    {
                        ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer = "B";
                    }
                    else if (((App)Application.Current).currentGame.currentAnswer.Equals("B"))
                    {
                        ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer = "C";
                    }
                    else
                    {
                        ((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer = "A";
                    }
                }

                //((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.generateAnswer(r.Next(3));
            }

            //make 1 mob get answer everytime
            //((App)Application.Current).currentGame.CurrentMob.ElementAt(99).Value.Answer = ((App)Application.Current).currentGame.currentAnswer;
        }

        private Boolean generateProbability(KeyValuePair<int, MobMember> member)
        {
            int probability = 0, temp = 0;

            probability = member.Value._IQ * 10;

            if (((App)Application.Current).currentGame.currentLevel < 5)
            {
                probability -= 10;
            }
            else if (((App)Application.Current).currentGame.currentLevel > 5 && ((App)Application.Current).currentGame.currentLevel < 9)
            {
                probability -= 20;
            }
            else
            {
                probability -= 30;
            }

            if (((App)Application.Current).currentGame.mobEliminated < 25)
            {
                probability += 40;
            }
            else if (((App)Application.Current).currentGame.mobEliminated >= 25 && ((App)Application.Current).currentGame.mobEliminated <= 50)
            {
                probability += 30;
            }
            else if (((App)Application.Current).currentGame.mobEliminated > 50 && ((App)Application.Current).currentGame.mobEliminated <= 80)
            {
                probability += 20;
            }
            else if (((App)Application.Current).currentGame.mobEliminated > 80)
            {
                probability += 10;
            }

            if (probability >= 100)
            {
                return true;
            }

            if (probability < 0)
            {
                return false;
            }

            probability /= 10;
            temp = r.Next(1, 11);

            if (temp <= probability)
            {
                return true;
            }

            return false;
        }

        private void lifeLine(int num)
        {
            int choiceA = 0, choiceB = 0, choiceC = 0, i = 0;

            if (!lifeLineSet)
            {
                generateMobAnswers();
                lifeLineSet = true;
            }
            
            for (i = 0; i < ((App)Application.Current).currentGame.CurrentMob.Count; i++)
            {
                if (!(((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Eliminated))
                {
                    if (((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer.Equals("A"))
                    {
                        choiceA++;
                    }
                    else if (((App)Application.Current).currentGame.CurrentMob.ElementAt(i).Value.Answer.Equals("B"))
                    {
                        choiceB++;
                    }
                    else
                    {
                        choiceC++;
                    }
                }
            }

            if (num == 1)
            {
                MessageBox.Show(choiceA.ToString() + " chose 'A'\n" + choiceB.ToString() + " chose 'B'\n" + choiceC.ToString() + " chose 'C'\n", "Mob's Poll");
                ((App)Application.Current).currentGame.currentPlayer.Line1 = true;
            }
            else
            {
                if ((choiceA > choiceB) && (choiceA > choiceC))
                {
                    if (num == 2)
                    {
                        MessageBox.Show("The majority of the mob chose 'A'", "Ask The Mob");
                        ((App)Application.Current).currentGame.currentPlayer.Line2 = true;
                    }
                    else
                    {
                        MessageBox.Show("The mob recommends choice 'A'", "Trust The Mob");
                        btnAnswer1.Focus();
                        ((App)Application.Current).currentGame.currentPlayer.Line3 = true;
                    }
                }
                else if ((choiceB > choiceA) && (choiceB > choiceC))
                {
                    if (num == 2)
                    {
                        MessageBox.Show("The majority of the mob chose 'B'", "Ask The Mob");
                        ((App)Application.Current).currentGame.currentPlayer.Line2 = true;
                    }
                    else
                    {
                        MessageBox.Show("The mob recommends choice 'B'", "Trust The Mob");
                        btnAnswer2.Focus();
                        ((App)Application.Current).currentGame.currentPlayer.Line3 = true;
                    }
                }
                else
                {
                    if (num == 2)
                    {
                        MessageBox.Show("The majority of the mob chose 'C'", "Ask The Mob");
                        ((App)Application.Current).currentGame.currentPlayer.Line2 = true;
                    }
                    else
                    {
                        MessageBox.Show("The mob recommends choice 'C'", "Trust The Mob");
                        btnAnswer3.Focus();
                        ((App)Application.Current).currentGame.currentPlayer.Line3 = true;
                    }
                }
            }
        }

        public void createProgressBar()
        {
            double timer = ((App)Application.Current).currentGame.currentRules.Timer;

            pbTimer.IsIndeterminate = false;

            pbTimer.Orientation = Orientation.Horizontal;

            pbTimer.Width = 620;

            pbTimer.Height = 20;

            Duration duration = new Duration(TimeSpan.FromSeconds(timer));

            DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);

            pbTimer.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);

            if (!statusBarSet)
            {
                sbTimer.Items.Add(pbTimer);
                pbTimer.ValueChanged += new RoutedPropertyChangedEventHandler<double>(pbTimer_ValueChanged);
            }

            statusBarSet = true;
        }
       
        private void playCorrect()
        {
            correctPlayer.Open(new Uri("correct.mp3", UriKind.Relative));
            correctPlayer.Play();
        }

        private void playWrong()
        {
            wrongPlayer.Open(new Uri("wrong.mp3", UriKind.Relative));
            wrongPlayer.Play();
        }

        public void playQuestionMusic()
        {
            questionPlayer.Open(new Uri("question.mp3", UriKind.Relative));
            questionPlayer.MediaEnded += new EventHandler(questionPlayer_MediaEnded);
            questionPlayer.Play();
        }

        public void enableLifeLine()
        {
            if (!(((App)Application.Current).currentGame.currentPlayer.Line1))
            {
                btnLifeLine1.IsEnabled = true;
            }
            if (!((App)Application.Current).currentGame.currentPlayer.Line2)
            {
                btnLifeLine2.IsEnabled = true;
            }
            if(!((App)Application.Current).currentGame.currentPlayer.Line3)
            {
                btnLifeLine3.IsEnabled = true;
            }
        }

        private void disableLifeLine()
        {
            btnLifeLine1.IsEnabled = false;
            btnLifeLine1.Foreground = Brushes.Black;
            btnLifeLine2.IsEnabled = false;
            btnLifeLine2.Foreground = Brushes.Black;
            btnLifeLine3.IsEnabled = false;
            btnLifeLine3.Foreground = Brushes.Black;

        }

        private void stopAnimation()
        {
            pbTimer.BeginAnimation(ProgressBar.ValueProperty, null);
        }


        /****************************************************Event Handling Methods */

        void questionPlayer_MediaEnded(object sender, EventArgs e)
        {
            questionPlayer.Position = new TimeSpan(0, 0, 0);
        }

        private void pbTimer_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.NewValue == 100.0)
            {
                questionPlayer.Stop();
                playWrong();
                selfClose = false;
                this.Close();
            }
        }

        private void btnAnswer1_Click(object sender, RoutedEventArgs e)
        {
            questionPlayer.Stop();
            if (((App)Application.Current).currentGame.currentAnswer.Equals("A"))
            {
                playCorrect();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer1.Background = Brushes.LawnGreen;
                lblAnswer1.Foreground = Brushes.Yellow;
                MessageBox.Show("Congratulations! It is correct", "Correct!");
                this.Visibility = Visibility.Hidden;
                lblAnswer1.Background = Brushes.Blue;
                lblAnswer1.Foreground = Brushes.Cyan;
            }
            else
            {
                playWrong();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer1.Background = Brushes.Red;
                lblAnswer1.Foreground = Brushes.Black;
                MessageBox.Show("Sorry!, that answer is incorrect.", "Wrong!");

                selfClose = false;
                this.Close();
            }
        }

        private void btnAnswer2_Click(object sender, RoutedEventArgs e)
        {
            questionPlayer.Stop();
            if (((App)Application.Current).currentGame.currentAnswer.Equals("B"))
            {
                playCorrect();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer2.Background = Brushes.LawnGreen;
                lblAnswer2.Foreground = Brushes.Yellow;
                MessageBox.Show("Congratulations! It is correct", "Correct!");
                this.Visibility = Visibility.Hidden;
                lblAnswer2.Background = Brushes.Blue;
                lblAnswer2.Foreground = Brushes.Cyan;
            }
            else
            {
                playWrong();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer2.Background = Brushes.Red;
                lblAnswer2.Foreground = Brushes.Black;
                MessageBox.Show("Sorry!, that answer is incorrect.", "Wrong!");

                selfClose = false;
                this.Close();
            }
        }

        private void btnAnswer3_Click(object sender, RoutedEventArgs e)
        {
            questionPlayer.Stop();
            if (((App)Application.Current).currentGame.currentAnswer.Equals("C"))
            {
                playCorrect();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer3.Background = Brushes.LawnGreen;
                lblAnswer3.Foreground = Brushes.Yellow;
                MessageBox.Show("Congratulations! It is correct", "Correct!");
                this.Visibility = Visibility.Hidden;
                lblAnswer3.Background = Brushes.Blue;
                lblAnswer3.Foreground = Brushes.Cyan;
            }
            else
            {
                playWrong();
                if (((App)Application.Current).currentGame.timer)
                {
                    stopAnimation();
                }
                lblAnswer3.Background = Brushes.Red;
                lblAnswer3.Foreground = Brushes.Black;
                MessageBox.Show("Sorry!, that answer is incorrect.", "Wrong!");

                selfClose = false;
                this.Close();
            }
        }

        private void btnLifeLine1_Click(object sender, RoutedEventArgs e)
        {
            if (((App)Application.Current).currentGame.timer)
            {
                stopAnimation();
            }
            lifeLine(1);
            disableLifeLine();
        }

        private void btnLifeLine2_Click(object sender, RoutedEventArgs e)
        {
            if (((App)Application.Current).currentGame.timer)
            {
                stopAnimation();
            }
            lifeLine(2);
            disableLifeLine();
        }

        private void btnLifeLine3_Click(object sender, RoutedEventArgs e)
        {
            if (((App)Application.Current).currentGame.timer)
            {
                stopAnimation();
            }
            lifeLine(3);
            disableLifeLine();
        }

        /*
        private void checkAnswer(String ans)
        {
            if (((App)Application.Current).currentGame.currentAnswer.Equals(ans))
            {
                lblAnswer1.Background = Brushes.LawnGreen;
                lblAnswer1.Foreground = Brushes.Yellow;
                MessageBox.Show("Congratulations! It is correct", "Correct!");
                ((App)Application.Current).currentGame.continueOn = true;
                this.Visibility = Visibility.Hidden;
            }
            else
            {
                lblAnswer1.Background = Brushes.Red;
                lblAnswer1.Foreground = Brushes.Black;
                MessageBox.Show("Sorry!, that answer is incorrect.", "Wrong!");
                ((App)Application.Current).currentGame.continueOn = false;
                this.Close();
            }
        }
        */ 

    }
}
